package com.yunji.app.common;

import com.yunji.common.base.BaseController;
import com.yunji.common.base.RuoYiConfig;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.utils.file.FileUploadUtils;
import com.yunji.common.utils.file.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @Package com.yunji.web.common
 * FileName:	GateWayUploadController.java
 * Copyright:	Copyright © 2024 云集互联 All Rights Reserved
 * Company:		云集互联
 * @version: 1.0
 * @since: JDK 1.8.0_202
 * Create at:   2024-04-26 19:19:46
 * Description:
 * 通用上传交互
 * Modification History:
 * Author					Date						Version			Description
 * --------------------------------------------------------------------------------
 * HuaAo				2024-04-26 19:19:46			1.0				1.0 Version
 */
@Api(tags = "移动端-通用上传")
@RestController
@RequestMapping("/gateway")
public class GateWayUploadController extends BaseController {
    @PostMapping("/upload")
    @ApiOperation("通用上传")
    public Response<Map<String,Object>> uploadFile(MultipartFile file) {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = RuoYiConfig.getHost() + fileName;
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("url", url);
            map.put("fileName", fileName);
            map.put("newFileName", FileUtils.getName(fileName));
            map.put("originalFilename", file.getOriginalFilename());
            return ResponseHelper.success(map);
        } catch (Exception e) {
            return ResponseHelper.failure(e.getMessage());
        }
    }

    @ApiOperation("服务监听")
    @PostMapping("/listener")
    public Response<String> listener() {
        return ResponseHelper.success();
    }
}